#ifndef __CPU_H_
#define __CPU_H_

#include <compiler.h>

#define sti() asm volatile("sti"::)
#define cli() asm volatile("cli"::)
#define nop() asm volatile("nop"::)
#define spin() asm volatile("rep; nop"::)

#define BOCHSBREAK asm volatile("xchg %%bx, %%bx"::);

static inline void cpu_flush_page(unsigned long va) {
    asm volatile("invlpg (%0)" ::"r"(va) : "memory");
}

static inline unsigned long cpu_rcr3() {
    unsigned long val;
    asm volatile("movl %%cr3,%0" : "=r"(val));
    return val;
}

static inline unsigned long cpu_rcr2() {
    unsigned long val;
    asm volatile("movl %%cr2,%0" : "=r"(val));
    return val;
}

static inline unsigned long cpu_rcr0() {
    unsigned long val;
    asm volatile("movl %%cr0,%0" : "=r"(val));
    return val;
}

static inline unsigned long cpu_reflags() {
    unsigned long val;
    asm volatile("pushf\n"
                 "popl %0\n"
                 : "=r"(val)::);
    return val;
}

#endif